package com.cgc999.host.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.cgc999.host.R;
import com.cgc999.host.activity.PromoteHomeActivity;
import com.cgc999.host.vo.PromoteInfoVo;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import org.xutils.x;

import java.util.List;

public class PromoteListAdapter extends XRecyclerView.Adapter<PromoteListAdapter.Holder> {
    private List<PromoteInfoVo> list;

    private Context context;

    public PromoteListAdapter(Context context, List<PromoteInfoVo> list) {
        this.list = list;
        this.context = context;
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.found_item, parent, false);
        Holder holder = new Holder(view);
        holder.txtTitle = view.findViewById(R.id.found_item_title);
        holder.txtDesc = view.findViewById(R.id.found_item_desc);
        holder.imageView = view.findViewById(R.id.found_item_image);
        return holder;
    }

    @Override
    public void onBindViewHolder(Holder holder, final int position) {
        holder.txtTitle.setText(list.get(position).getName());
        holder.txtDesc.setText(list.get(position).getDescripiton());
        x.image().bind(holder.imageView, list.get(position).getQr_url());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, PromoteHomeActivity.class);
                intent.putExtra("promote_id",list.get(position).getPromote_id());
                intent.putExtra("promote_name",list.get(position).getName());
//                intent.putExtra("url", "https://www.baidu.com");
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }


    static class Holder extends RecyclerView.ViewHolder {
        private TextView txtTitle,txtDesc;
        private ImageView imageView;

        //        private View line;
        public Holder(View itemView) {
            super(itemView);
            txtTitle = itemView.findViewById(R.id.found_item_title);
            txtDesc = itemView.findViewById(R.id.found_item_desc);
            imageView = itemView.findViewById(R.id.found_item_image);
//            line = itemView.findViewById(R.id.found_item_line);
        }
    }
}
